package test.fun;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;

import java.io.IOException;

import static test.fun.GetConnet.connection;

/**
 * ClassName: ScanFun
 * Package: test.fun
 * Description:
 *
 * @Author ChenJun
 * @Create 2023/4/25 8:39
 * @Version 1.0
 */
public class ScanFun {
    public static void scanTable(String tableName, String start, String stop) throws IOException {

        //获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建Scan对象
        Scan scan = new Scan();
        scan.withStartRow(start.getBytes());
        scan.withStopRow(stop.getBytes());

        //创建过滤器对象
        ValueFilter valueFilter = new ValueFilter(
                CompareOperator.EQUAL,
                new SubstringComparator("zhang")
        );

        //过滤器集合
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        filterList.addFilter(valueFilter);
        filterList.addFilter(valueFilter);

        //添加过滤器
        scan.setFilter(valueFilter);

        //执行扫描全表
        ResultScanner results = table.getScanner(scan);

        //解析ResultScanner
        for (Result result : results) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("RK:" + new String(CellUtil.cloneRow(cell)) +
                        ",CF:" + new String(CellUtil.cloneFamily(cell)) +
                        ",CN:" + new String(CellUtil.cloneQualifier(cell)) +
                        ",Value:" + new String(CellUtil.cloneValue(cell)));
            }
        }
    }
}
